package com.slf.engine.threads;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import com.slf.engine.bo.Order;
import com.slf.engine.common.SysContext;
import com.slf.engine.services.ITjfxConnService;
import com.slf.engine.services.helper.ServiceHelper;
import com.slf.engine.services.impl.BusServiceImpl;
import com.slf.engine.services.impl.TjfxConnServiceImpl;
import com.slf.engine.utils.SocketUtil;

public class RecvThread extends Thread {

	private static final Logger log = Logger.getLogger(RecvThread.class);
	
	public RecvThread()
	{
		super("RECV_THREAD");
		log.log(Priority.INFO, "RECV_THREAD异步接收线程启动");
	}
	
	public void run()
	{
		doRecv();
	}
	
	public void doRecv()
	{
		InputStream in = null;
		while(true)
		{
			try {
				in = SocketUtil.getInStantClient().getInputStream();
				
				byte b[] = new byte[SocketUtil.getInStantClient().getReceiveBufferSize()];
				String msg = "";
				if(in.read(b) != -1)
				{
					msg = new String(b);
				}
				log.log(Priority.INFO, "recvMsg:" + msg.trim());
				if(msg.length() < 4)
				{
					SocketUtil.closeSocket();
					SysContext.IS_START = false;
					continue;
				}
				List<String> list = ServiceHelper.splitRecvMsg(msg);
				for(int i = 0; i < list.size(); i++)
				{
					doBackService(list.get(i));
				}
			}catch (IOException e) {
				// TODO Auto-generated catch block
				log.log(Priority.ERROR, "接收线程创建错误，3秒后重试");
				try {
					Thread.sleep(3000);
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
				}
//				SocketUtil.closeSocket();
			}catch (Exception e){
				log.log(Priority.ERROR, "recv_error:" + e.getMessage());
			}
		} 
	}
	
	private boolean doBackService(String msg)
	{
//		IZjzhService service = null;
//		String head[] = ServiceHelper.backMsgHead(msg);
		ITjfxConnService service = null;
		Order order = null;
		SysContext.FREE_TIME = System.currentTimeMillis();
		int type = Integer.parseInt(msg.substring(8, 12));
		switch (type) {
		case 101: //登录
			if("00".equals(msg.substring(32, 34)))
			{
				//登录成功
				log.log(Priority.INFO, "上游服务连接成功");
				SysContext.HEART_TIME = 0;
				SysContext.IS_START = true;
			}
			break;
//		case 800119: //心跳
//			if(!"0000".equals(head[4]))
//			{
//				//心跳处理失败
//				log.log(Priority.INFO, "心跳连接失败,执行重新登录");
//				SysContext.IS_START = false;
//				IZjzhService zjzhService = new ZjzhServiceImpl();
//				zjzhService.doLogin();
//			}else{
//				SysContext.HEART_TIME = 0;
//				log.log(Priority.INFO, "心跳连接成功");
//			}
//			break;
		case 201: //统一扣款
			service = new TjfxConnServiceImpl();
			order = service.backBusMsg(msg);
			if(order != null)
			{
				new BusServiceImpl().doBusResult(order);
			}
			break;
		case 311: //统一扣款
			service = new TjfxConnServiceImpl();
			order = service.backQueryMsg(msg);
			if(order != null)
			{
				new BusServiceImpl().doBusResult(order);
			}
			break;
		case 110: //通用错误
			
			break;
		}
		return false;
	}
}
